<?php
/**
 * Descript:
 * User: blue
 * Date: 2018/9/10 18:36
 */

namespace app\library\except;


use app\library\enum\ErrDict;
use think\exception\Handle;
use think\facade\Log;

class ExceptionHandler extends Handle
{
    /**
     * 自定义异常处理
     * @param \Exception $e
     * @return \think\Response|\think\response\Json
     */
    public function render(\Exception $e)
    {
        if($e instanceof BaseException){
            $err=[
                'errCode'=>$e->errCode,
                'message'=>$e->message
            ];
            $httpCode=$e->code;
        }else{
            if(config('app.app_debug')){
                return parent::render($e);
            }else{
                $err=[
                    'errCode'=>ErrDict::BASE_SERVER_ERROR,
                    'message'=>'服务器内部错误'
                ];
                $httpCode=500;
                Log::write($e->getMessage(),'error');
            }
        }
        $err['url']=request()->url();
        return json($err,$httpCode);
    }
}